package ord2.impl.indexer;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import ord2.core.Parameter;
import ord2.crawler.Crawler;
import ord2.crawler.CrawlerListener;
import ord2.indexer.BlockingAbstractIndexer;
import org.apache.lucene.analysis.br.BrazilianAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.*;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import static org.apache.lucene.document.Field.Index;
import static org.apache.lucene.document.Field.Store;
import static org.apache.lucene.document.Field.TermVector;
/**
 *
 * @author José Ivan
 * @author Edviges
 */
public class LuceneIndexer extends BlockingAbstractIndexer implements CrawlerListener {

    //TODO Add buffer de indexes
    private IndexWriter iw;
    
    public LuceneIndexer(Parameter[] pars) {
        
        super(pars);
    }
    
    public LuceneIndexer(File[] docs, Parameter[] pars) {
        
        super(docs, pars);

        try {
            
            SimpleFSDirectory dir = new SimpleFSDirectory(getIndexDir());
            IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_31,
                    new BrazilianAnalyzer(Version.LUCENE_31));
            iw = new IndexWriter(dir, iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE));
            iw.optimize();
            iw.close();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }
    
    @Override
    public synchronized boolean processDocuments() {
        
        if (isProcessing()) {
            
            return false;
        }
        
        super.notifyStartProcessing();
        //Parameter[] pars = getParameters();

        try {

            SimpleFSDirectory dir = new SimpleFSDirectory(getIndexDir());
            IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_31,
                    new BrazilianAnalyzer(Version.LUCENE_31));
            iw = new IndexWriter(dir, iwc.setOpenMode(IndexWriterConfig.OpenMode.APPEND));
            for (File f : getAllDocuments()) {

                String[] parts = f.getName().split("[?]");
                Field fp = new Field("path", "http://www.in.gov.br/imprensa/visualiza/index.jsp?" + parts[1],
                        Store.YES, Index.NOT_ANALYZED);
                //Field fp = new Field("path", f.getAbsolutePath(), Store.YES, Index.NOT_ANALYZED);
                Field fc = new Field("content", new FileReader(f), TermVector.YES);
                Document doc = new Document();
                doc.add(fp);
                doc.add(fc);
                iw.addDocument(doc);
                removeDocument(f);
            }
        
            iw.optimize();
            iw.close();
        } catch (IOException io) {
            
            super.notifyStopProcessing();
            return false;
        }
        
        super.notifyStopProcessing();
        return true;
    }

    //Pode não ser chamado
    @Override
    protected void finalize() throws Throwable {

        super.finalize();
        iw.close();
    }
    @Override
    public void operationAction(int type, Crawler crawler) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void urlAction(int type, Crawler crawler, URL url) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void documentAction(Crawler crawler, File document) {

        this.addDocument(document);
        processDocuments();
    }
}
